// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        speed: 10,
        distance: 200,

        //添加进度条
        enemyBar: {
            type: cc.Node,
            default: null,
        },

        //添加文字label
        hplabel: {
            type: cc.Label,
            default: null,
        },

        enemybullet: {
            type: cc.Prefab,
            default: null,
        },
        diebgm:{
            default:null,
            type: cc.AudioClip,
        },
    },


    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this._movedDistance = this.distance / 2;
        this._direction = 1;
        this._movedDiff = 0;
        //cc.director.getPhysicsManager().enabled = true;//开启物理系统
        //cc.director.getCollisionManager().enabled = true//开启碰撞组件

        this.hp = 1000;

        this.time = 0;

    },

    start() {

    },

    jianxue() {
        var pp = this.enemyBar.getComponent(cc.ProgressBar);//取得进度跳控件
        pp.totalLength -= 0.1;//改变进度条（即血条）
        this.hp -= 1;//改变雪值（即string）
        this.hplabel.string = this.hp.toString();//当前HP=文字string
        if (pp.totalLength <= 0) {
            this.node.destroy();//删除飞机
            cc.audioEngine.playEffect(this.diebgm, false);
            cc.director.loadScene('game2');//切换场景
        }
    },

    update(dt) {
        var d = this.speed * this._direction * dt;

        var movedDistance = this._movedDistance + Math.abs(d);
        this._movedDistance += Math.abs(d);

        if (movedDistance > this.distance) {
            d = this.distance - this._movedDistance;
            this._movedDistance = 0;
            this._direction *= -1;
        }
        else {
            this._movedDistance = movedDistance;
        }

        this.node.x += d;
        this._movedDiff = d;

        this.time++;
        if (this.time == 50) {
            var bulletNode = cc.instantiate(this.enemybullet);//生成子弹//预制体实例化
            this.node.addChild(bulletNode);//子弹添加到父节点
            bulletNode.setPosition(0, -131);//子弹位置
            var rgb = bulletNode.getComponent(cc.RigidBody);//声明子弹的刚体
            rgb.applyLinearImpulse(cc.v2(0, -50), rgb.getWorldCenter());//给子弹设置一个向下的冲量

            this.time = 0;
        }


        // cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = false;
    },
});
